package com.novanok.mailtweets.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.novanok.mailtweets.R;
import com.novanok.mailtweets.comun.Comun;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import twitter4j.DirectMessage;
import twitter4j.Status;

public class ListaMensajeDirectoAdapter extends BaseAdapter {

    ArrayList<DirectMessage> myList = new ArrayList<DirectMessage>();
    LayoutInflater inflater;
    Context context;

    public ListaMensajeDirectoAdapter(Context context, ArrayList<DirectMessage> myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from( this.context );
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myList.size();
    }

    @Override
    public DirectMessage getItem(int arg0) {
        // TODO Auto-generated method stub
        return myList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView( int arg0, View arg1, ViewGroup arg2 ) {
        if (arg1 == null) {
            arg1 = inflater.inflate( R.layout.list_item_mensaje_directo, arg2, false );
        }

        ImageView image = (ImageView)arg1.findViewById(R.id.imgProfile);
        TextView usuario = (TextView)arg1.findViewById(R.id.txtUsuario);
        TextView mensaje = (TextView)arg1.findViewById(R.id.txtMensaje);

        synchronized ( myList ) {
            DirectMessage msg = myList.get(arg0);

            image.setImageBitmap( Comun.getBitmapFromURL(msg.getSender().getBiggerProfileImageURL()) );
            usuario.setText("@" + msg.getSender().getScreenName());
            mensaje.setText(msg.getText());
        }

        return arg1;
    }

}